﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Adams.Web.Controllers
{
    [EnsureSsl]
    public class SecurityController : Controller
    {
        [AppendLayoutModel]
        public ActionResult Login()
        {
            var model = Logic.Security.Login.PageLoad();
            return View(MobileCheck.IsMobile() ? "Login.Mobile" : "Login", model);
        }

        public JsonResult TryLogin(Adams.Web.Models.Security.Login.FormInput input)
        {
            var model = Logic.Security.Login.TryLogin(input);

            if (model.IsValid)
            {
                FormsAuthentication.SetAuthCookie(model.MemberID.ToString(), input.RememberMe.ToBool());
            }

            return Json(model);
        }

        public ActionResult Logout()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            return Redirect("/");
        }

        [AppendLayoutModel]
        public ActionResult Forgot(string email)
        {
            var model = Logic.Security.Forgot.PageLoad(email);
            return View(MobileCheck.IsMobile() ? "Forgot.Mobile" : "Forgot", model);
        }

        public JsonResult TryForgot(string captcha, Adams.Web.Models.Security.Forgot.FormInput input)
        {
            string code = Request.Cookies["code"].Value;
            Request.Cookies["code"].Expires = DateTime.Now.AddDays(-1);

            if (Logic.Security.Captcha.ValidateCaptcha(captcha, code))
            {
                var model = Logic.Security.Forgot.TryForgot(input);
                return Json(model);
            }
            else
            {
                var model = new Models.FormOutput();
                model.InvalidFields.Add(new Models.InvalidField() { Code = 26, Name = "Captcha" });
                return Json(model);
            }
        }

        [AppendLayoutModel]
        public ActionResult ForgotComplete(string email)
        {
            var model = Logic.Security.ForgotComplete.PageLoad(email);
            return View(MobileCheck.IsMobile() ? "ForgotComplete.Mobile" : "ForgotComplete", model);
        }

        [AppendLayoutModel]
        public ActionResult Signup()
        {
            var model = Logic.Security.Signup.PageLoad();
            return View(MobileCheck.IsMobile() ? "Signup.Mobile" : "Signup", model);
        }

        public JsonResult TrySignup(string captcha, Adams.Web.Models.Security.Signup.FormInput input)
        {
            string code = Request.Cookies["code"].Value;
            Request.Cookies["code"].Expires = DateTime.Now.AddDays(-1);

            if (Logic.Security.Captcha.ValidateCaptcha(captcha, code))
            {
                var model = Logic.Security.Signup.TrySignup(input);
                return Json(model);
            }
            else
            {
                var model = new Models.FormOutput();
                model.InvalidFields.Add(new Models.InvalidField() { Code = 26, Name = "Captcha" });
                return Json(model);
            }
        }

        [AppendLayoutModel]
        public ActionResult SignupComplete(string email)
        {
            var model = Logic.Security.SignupComplete.PageLoad(email);
            return View(MobileCheck.IsMobile() ? "SignupComplete.Mobile" : "SignupComplete", model);
        }

        [AppendLayoutModel]
        public ActionResult ConfirmEmail(string code)
        {
            var model = Logic.Security.ConfirmEmail.PageLoad(code);
            return View(model);
        }

        [AppendLayoutModel]
        public ActionResult PasswordReset(string code)
        {
            var model = Logic.Security.PasswordReset.PageLoad(code);
            return View(MobileCheck.IsMobile() ? "PasswordReset.Mobile" : "PasswordReset", model);
        }

        public JsonResult TryPasswordReset(Adams.Web.Models.Security.PasswordReset.FormInput input)
        {
            var model = Logic.Security.PasswordReset.TryPasswordReset(input);
            return Json(model);
        }

        [AppendLayoutModel]
        public ActionResult Resend(string email)
        {
            var model = Logic.Security.Resend.PageLoad(email);
            return View(MobileCheck.IsMobile() ? "Resend.Mobile" : "Resend", model);
        }

        public JsonResult TryResend(string captcha, Adams.Web.Models.Security.Resend.FormInput input)
        {
            string code = Request.Cookies["code"].Value;
            Request.Cookies["code"].Expires = DateTime.Now.AddDays(-1);

            if (Logic.Security.Captcha.ValidateCaptcha(captcha, code))
            {
                var model = Logic.Security.Resend.TryResend(input);
                return Json(model);
            }
            else
            {
                var model = new Models.FormOutput();
                model.InvalidFields.Add(new Models.InvalidField() { Code = 26, Name = "Captcha" });
                return Json(model);
            }
        }

        [AppendLayoutModel]
        public ActionResult ResendComplete(string email)
        {
            var model = Logic.Security.ResendComplete.PageLoad(email);
            return View(MobileCheck.IsMobile() ? "ResendComplete.Mobile" : "ResendComplete", model);
        }

        public CaptchaResult Captcha()
        {
            string randomText = Shared.Str.GenerateRandomString(6);
            var code = Logic.Security.Captcha.GenerateCaptcha(randomText);
            HttpCookie hc = new HttpCookie("code", code);
            hc.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(hc);

            return new CaptchaResult(randomText);
        }
    }
}
